#include <config.h>
#include <pic_operation.h>
#include <picfmt_manager.h>
#include <string.h>


static PT_PicFileParser g_ptPicFileParser;

int RegisterFileParser(PT_PicFileParser ptPicFileParser)
{
	PT_PicFileParser ptTmp;

	if (!g_ptPicFileParser)
	{
		g_ptPicFileParser = ptPicFileParser;
		ptPicFileParser->ptNext = NULL;
	}
	else
	{
		ptTmp = g_ptPicFileParser;
		while(ptTmp->ptNext)
		{
			ptTmp = ptTmp->ptNext;
		}
		ptTmp->ptNext = ptPicFileParser;
		ptPicFileParser->ptNext = NULL;
	}
	return 0;
}

void ShowPicFmt(void)
{
	int i = 0;
	PT_PicFileParser ptTmp = g_ptPicFileParser;
	
	while(ptTmp)
	{
		printf("%02d %s\n", i++, ptTmp->name);
		ptTmp = ptTmp->ptNext;
	}
}

PT_PicFileParser Parser(char *name)
{
	PT_PicFileParser ptTmp = g_ptPicFileParser;
	
	while(ptTmp)
	{
		if (strcmp(ptTmp->name, name) == 0)
		{
			return ptTmp;
		}
		ptTmp = ptTmp->ptNext;
	}
	return NULL;
}

PT_PicFileParser GetParser(PT_FileMap ptFileMap)
{
	PT_PicFileParser ptTmp = g_ptPicFileParser;
	
	while(ptTmp)
	{
		if(ptTmp->isSupport(ptFileMap))
		{
			return ptTmp;
		}
		ptTmp = ptTmp->ptNext;
	}
	return NULL;
}

int PicFmtsInit(void)
{
	int iError;

	iError = BMPRegister();
	iError |= JPGRegister();

	return iError;
}


